package cobots.swarm.main;



import java.util.LinkedList;



public class InteractionHandler extends Thread {

   
    protected LinkedList<OperatorAction> actions = new LinkedList<OperatorAction>();
    
    protected Object id = this;
  
    
    public void addAction(OperatorAction action) {
        synchronized (actions) {
            actions.add(action);    
            
        }    	
    }

    
   
   public void run() {
       
        while (true) {
        	
        	try {		
        	    OperatorAction action;
        	    synchronized (actions) {
        	        action = actions.poll();
        	       
        	        if (action == null) {
        	            Thread.yield();
        	           
        	            continue;
        	        }
                    }                

             //  switch (action.getType()) {
                
               // case OperatorAction.SUPPRESS_TARGET:
                    short targetId = action.getTargetId();
                    short leaderId = action.getLeaderId();
                    float radius = action.getAttractionRadius();
                    SwarmState.target(targetId).reactionTime = SwarmState.currentTime;
                    SwarmState.target(targetId).setLeaderId(leaderId);
                    if (SwarmState.leader(leaderId).getTargetId() == -1) { // only if it's not already following
                        SwarmState.leader(leaderId).setTargetId(targetId);
                        SwarmState.leader(leaderId).setAttractionRadius(radius);
                        SwarmState.leader(leaderId).setComputedCoalition(false);
                    }			
               //     break;
                    
               //  }
            }
           catch (Exception e) {
            	e.printStackTrace();
               
                break;
            }
        }
    }
}
